//
//  MovieScene.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 12/3/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "MovieScene.h"
#import "MenuScene.h"

@implementation MovieScene

+(CCScene *) scene
{
    CCScene *scene = [CCScene node];
    MovieScene *layer = [MovieScene node];
    [scene addChild:layer];
    return scene;
}

-(id) init
{
    if (self = [super init])
    {
        winSize = [[CCDirector sharedDirector] winSize];
        [self step1];
    }
    
    return self;
}

-(void) step1
{
    CCSprite *spr1 = [CCSprite spriteWithSpriteFrameName:SFN_PNODE];
    [spr1 setColor:CL_PANEL_BTN];
    [spr1 setPosition:ccp(winSize.width / 2, uniPos(-200, -400))];
    [self addChild:spr1 z:0 tag:1];
    
    CCJumpTo *jumpTo1 = [CCJumpTo actionWithDuration:4 position:ccp(winSize.width / 2, winSize.height / 2) height:20 jumps:10];
    [spr1 runAction:jumpTo1];
    
    CCSprite *spr2 = [CCSprite spriteWithSpriteFrameName:SFN_PNODE];
    [spr2 setColor:CL_PANEL_BTN];
    [spr2 setPosition:ccp(winSize.width / 2 - uniPos(30, 60), uniPos(-230, -460))];
    [self addChild:spr2 z:0 tag:2];
    
    CCJumpTo *jumpTo2 = [CCJumpTo actionWithDuration:4 position:ccp(winSize.width / 2 - uniPos(30, 60), winSize.height / 2 - uniPos(30, 60)) height:20 jumps:10];
    [spr2 runAction:jumpTo2];
    
    CCSprite *spr3 = [CCSprite spriteWithSpriteFrameName:SFN_PNODE];
    [spr3 setColor:CL_PANEL_BTN];
    [spr3 setPosition:ccp(winSize.width / 2 + uniPos(30, 60), uniPos(-230, -460))];
    [self addChild:spr3 z:0 tag:3];
    
    CCJumpTo *jumpTo3 = [CCJumpTo actionWithDuration:4 position:ccp(winSize.width / 2 + uniPos(30, 60), winSize.height / 2 - uniPos(30, 60)) height:20 jumps:10];
    [spr3 runAction:jumpTo3];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:4];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self step2];
    }];
    
    CCSequence *sequence  = [CCSequence actions:delay, callBlock, nil];
    [self runAction:sequence];
}

-(void) step2
{
    CCNode *spr1 = [self getChildByTag:1];
    CCNode *spr2 = [self getChildByTag:2];
    CCNode *spr3 = [self getChildByTag:3];
    
    [self text:@"bleh" delay:0.5 object:spr1];
    [self text:@"bloh" delay:1 object:spr2];
    [self text:@"blah" delay:1.5 object:spr3];
    [self text:@"^^" delay:3.5 object:spr1];
    [self text:@"o.o" delay:4 object:spr2];
    [self text:@"\(`O')/" delay:4.5 object:spr3];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:2];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self step3];
    }];
    
    CCSequence *sequence  = [CCSequence actions:delay, callBlock, nil];
    [self runAction:sequence];
}

-(void) step3
{
    CCSprite *spr1 = [CCSprite spriteWithSpriteFrameName:SFN_PNODE];
    [spr1 setColor:CL_PANEL_BTN];
    [spr1 setPosition:ccp(winSize.width / 2, -300)];
    [self addChild:spr1 z:0 tag:100];
    
    CCJumpTo *jumpTo1 = [CCJumpTo actionWithDuration:4 position:ccp(winSize.width / 2, winSize.height / 2 - uniPos(100, 200)) height:20 jumps:10];
    [spr1 runAction:jumpTo1];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:4];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self step4];
    }];
    
    CCSequence *sequence  = [CCSequence actions:delay, callBlock, nil];
    [self runAction:sequence];
}

-(void) step4
{
    CCNode *spr = [self getChildByTag:100];
    [self text:@"Hey, can i join?" delay:0.5 object:spr];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:2];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self step5];
    }];
    
    CCSequence *sequence  = [CCSequence actions:delay, callBlock, nil];
    [self runAction:sequence];
}

-(void) step5
{
    CCNode *spr1 = [self getChildByTag:1];
    CCNode *spr2 = [self getChildByTag:2];
    CCNode *spr3 = [self getChildByTag:3];
    
    CCMoveBy *moveBy1 = [CCMoveBy actionWithDuration:1 position:ccp(0, uniPos(100, 200))];
    CCMoveBy *moveBy2 = [CCMoveBy actionWithDuration:1 position:ccp(0, uniPos(100, 200))];
    CCMoveBy *moveBy3 = [CCMoveBy actionWithDuration:1 position:ccp(0, uniPos(100, 200))];
    
    [spr1 runAction:moveBy1];
    [spr2 runAction:moveBy2];
    [spr3 runAction:moveBy3];
    
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:1];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self step6];
    }];
    
    CCSequence *sequence  = [CCSequence actions:delay, callBlock, nil];
    [self runAction:sequence];
}

-(void) step6
{
    CCNode *spr = [self getChildByTag:1];
    [self text:@"Stay away from us, freak!" delay:0.5 object:spr];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:3];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self step7];
    }];
    
    CCSequence *sequence  = [CCSequence actions:delay, callBlock, nil];
    [self runAction:sequence];
}

-(void) step7
{
    CCNode *spr1 = [self getChildByTag:1];
    CCNode *spr2 = [self getChildByTag:2];
    CCNode *spr3 = [self getChildByTag:3];
    
    CCJumpTo *jumpTo1 = [CCJumpTo actionWithDuration:4 position:ccp(spr1.position.x, winSize.height + uniPos(400, 800)) height:20 jumps:10];
    CCJumpTo *jumpTo2 = [CCJumpTo actionWithDuration:4 position:ccp(spr2.position.x, winSize.height + uniPos(400, 800)) height:20 jumps:10];
    CCJumpTo *jumpTo3 = [CCJumpTo actionWithDuration:4 position:ccp(spr3.position.x, winSize.height + uniPos(400, 800)) height:20 jumps:10];
    
    [spr1 runAction:jumpTo1];
    [spr2 runAction:jumpTo2];
    [spr3 runAction:jumpTo3];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:3];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self step9];
    }];
    
    CCSequence *sequence  = [CCSequence actions:delay, callBlock, nil];
    [self runAction:sequence];
}

-(void) step9
{
    CCNode *spr1 = [self getChildByTag:100];
    CCJumpTo *jumpTo1 = [CCJumpTo actionWithDuration:4 position:ccp(spr1.position.x, winSize.height + 200) height:20 jumps:10];
    [spr1 runAction:jumpTo1];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:3];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self step11];
    }];
    
    CCSequence *sequence  = [CCSequence actions:delay, callBlock, nil];
    [self runAction:sequence];
}

-(void) step11
{
    CCNode *spr1 = [self getChildByTag:1];
    CCNode *spr2 = [self getChildByTag:2];
    CCNode *spr3 = [self getChildByTag:3];
    
    [spr1 setPosition:ccp(spr1.position.x, - 200)];
    [spr2 setPosition:ccp(spr2.position.x, - uniPos(230, 260))];
    [spr3 setPosition:ccp(spr3.position.x, - uniPos(230, 260))];
    
    CCJumpTo *jumpTo1 = [CCJumpTo actionWithDuration:4 position:ccp(spr1.position.x, winSize.height / 2) height:20 jumps:10];
    CCJumpTo *jumpTo2 = [CCJumpTo actionWithDuration:4 position:ccp(spr2.position.x, winSize.height / 2 - uniPos(30, 60)) height:20 jumps:10];
    CCJumpTo *jumpTo3 = [CCJumpTo actionWithDuration:4 position:ccp(spr3.position.x, winSize.height / 2 - uniPos(30, 60)) height:20 jumps:10];
    
    [spr1 runAction:jumpTo1];
    [spr2 runAction:jumpTo2];
    [spr3 runAction:jumpTo3];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:3];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self step13];
    }];
    
    CCSequence *sequence  = [CCSequence actions:delay, callBlock, nil];
    [self runAction:sequence];
}

-(void) step13
{
    CCSprite *sprB = [CCSprite spriteWithSpriteFrameName:SFN_BOSS_BODY];
    CCSprite *sprE = [CCSprite spriteWithSpriteFrameName:SFN_BOSS_EYE];
    CCSprite *sprV1 = [CCSprite spriteWithSpriteFrameName:SFN_VIRUS];
    CCSprite *sprV2 = [CCSprite spriteWithSpriteFrameName:SFN_VIRUS];
    
    [sprB setPosition:ccp(winSize.width / 2, winSize.height + 200)];
    [sprE setPosition:ccp(sprB.contentSize.width / 2, sprB.contentSize.height / 2)];
    [sprB addChild:sprE];
    
    [sprV1 setPosition:ccp(sprB.position.x - uniPos(50, 100), sprB.position.y + uniPos(10, 20))];
    [sprV2 setPosition:ccp(sprB.position.x + uniPos(50, 100), sprB.position.y + uniPos(10, 20))];
    
    [self addChild:sprB z:0 tag:50];
    [self addChild:sprV1 z:0 tag:61];
    [self addChild:sprV2 z:0 tag:62];
    
    CCMoveTo *moveTo = [CCMoveTo actionWithDuration:3 position:ccp(winSize.width / 2, winSize.height - uniPos(100, 200))];
    [sprB runAction:moveTo];
    
    CCMoveTo *moveTo1 = [CCMoveTo actionWithDuration:3 position:ccp(sprV1.position.x, winSize.height - uniPos(90, 180))];
    [sprV1 runAction:moveTo1];
    
    CCMoveTo *moveTo2 = [CCMoveTo actionWithDuration:3 position:ccp(sprV2.position.x, winSize.height - uniPos(90, 180))];
    [sprV2 runAction:moveTo2];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:4];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self step15];
    }];
    
    CCSequence *sequence  = [CCSequence actions:delay, callBlock, nil];
    [self runAction:sequence];
}

-(void) step15
{
    CCSprite *sprB = (CCSprite *)[self getChildByTag:50];
    CCSprite *sprI = [CCSprite spriteWithSpriteFrameName:SFN_NODE_INDICATOR];
    [sprI setScale:sprB.contentSize.width / uniPos(16, 32) / 2];
    [sprI setPosition:sprB.position];
    [sprI setOpacity:0];
    [self addChild:sprI z:0 tag:51];
    
    CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:1 opacity:255];
    [sprI runAction:fadeTo];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:1];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self step17];
    }];
    
    CCSequence *sequence  = [CCSequence actions:delay, callBlock, nil];
    [self runAction:sequence];
}

-(void) step17
{
    CCSprite *spr1 = (CCSprite *)[self getChildByTag:1];
    CCSprite *spr2 = (CCSprite *)[self getChildByTag:2];
    CCSprite *spr3 = (CCSprite *)[self getChildByTag:3];
    CCSprite *sprB = (CCSprite *)[self getChildByTag:50];
    
    CCSprite *spr4 = [CCSprite spriteWithSpriteFrameName:SFN_PNODE];
    [spr4 setColor:CL_PANEL_BTN];
    [spr4 setPosition:spr1.position];
    [self addChild:spr4 z:0 tag:4];
    
    CCSprite *spr5 = [CCSprite spriteWithSpriteFrameName:SFN_PNODE];
    [spr5 setColor:CL_PANEL_BTN];
    [spr5 setPosition:spr2.position];
    [self addChild:spr5 z:0 tag:5];
    
    CCSprite *spr6 = [CCSprite spriteWithSpriteFrameName:SFN_PNODE];
    [spr6 setColor:CL_PANEL_BTN];
    [spr6 setPosition:spr3.position];
    [self addChild:spr6 z:0 tag:6];
    
    [spr1 setColor:ccWHITE];
    [spr2 setColor:ccWHITE];
    [spr3 setColor:ccWHITE];
    
    
    CCMoveTo *moveTo1 = [CCMoveTo actionWithDuration:2 position:sprB.position];
    CCScaleTo *scaleTo1 = [CCScaleTo actionWithDuration:2 scale:0];
    CCEaseExponentialOut *ease1 = [CCEaseBackIn actionWithAction:scaleTo1];
    CCSpawn *spawn1 = [CCSpawn actions:moveTo1, ease1, nil];
    [spr4 runAction:spawn1];
    
    CCMoveTo *moveTo2 = [CCMoveTo actionWithDuration:2 position:sprB.position];
    CCScaleTo *scaleTo2 = [CCScaleTo actionWithDuration:2 scale:0];
    CCEaseExponentialOut *ease2 = [CCEaseBackIn actionWithAction:scaleTo2];
    CCSpawn *spawn2 = [CCSpawn actions:moveTo2, ease2, nil];
    [spr5 runAction:spawn2];
    
    CCMoveTo *moveTo3 = [CCMoveTo actionWithDuration:2 position:sprB.position];
    CCScaleTo *scaleTo3 = [CCScaleTo actionWithDuration:2 scale:0];
    CCEaseExponentialOut *ease3 = [CCEaseBackIn actionWithAction:scaleTo3];
    CCSpawn *spawn3 = [CCSpawn actions:moveTo3, ease3, nil];
    [spr6 runAction:spawn3];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:4];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self step19];
    }];
    
    CCSequence *sequence  = [CCSequence actions:delay, callBlock, nil];
    [self runAction:sequence];
}

-(void) step19
{
    CCSprite *spr = (CCSprite *)[self getChildByTag:100];
    CCSprite *sprV1 = (CCSprite *)[self getChildByTag:61];
    CCSprite *sprV2 = (CCSprite *)[self getChildByTag:62];
    CCSprite *sprI = (CCSprite *)[self getChildByTag:51];
    CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:1 opacity:0];
    [sprI runAction:fadeTo];
    
    CCMoveTo *moveTo1 = [CCMoveTo actionWithDuration:1 position:ccp(sprV1.position.x, winSize.height / 2 - uniPos(60, 120))];
    [sprV1 runAction:moveTo1];
    
    CCMoveTo *moveTo2 = [CCMoveTo actionWithDuration:1 position:ccp(sprV2.position.x, winSize.height / 2 - uniPos(60, 120))];
    [sprV2 runAction:moveTo2];
    
    [spr setPosition:ccp(winSize.width / 2, uniPos(-100, -200))];
    CCJumpTo *jumpTo = [CCJumpTo actionWithDuration:2 position:ccp(spr.position.x, uniPos(100, 200)) height:20 jumps:4];
    [spr runAction:jumpTo];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:2.5];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self step21];
    }];
    
    CCSequence *sequence  = [CCSequence actions:delay, callBlock, nil];
    [self runAction:sequence];

}

-(void) step21
{
    
    CCSprite *sprV1 = (CCSprite *)[self getChildByTag:61];
    CCSprite *sprV2 = (CCSprite *)[self getChildByTag:62];
    CCSprite *sprB = (CCSprite *)[self getChildByTag:50];
    CCSprite *spr1 = (CCSprite *)[self getChildByTag:1];
    CCSprite *spr2 = (CCSprite *)[self getChildByTag:2];
    CCSprite *spr3 = (CCSprite *)[self getChildByTag:3];
    
    CCMoveBy *mv1 = [CCMoveBy actionWithDuration:8 position:ccp(0, uniPos(1000, 2000))];
    CCMoveBy *mv2 = [CCMoveBy actionWithDuration:8 position:ccp(0, uniPos(1000, 2000))];
    CCMoveBy *mb = [CCMoveBy actionWithDuration:8 position:ccp(0, uniPos(1000, 2000))];
    CCMoveBy *m1 = [CCMoveBy actionWithDuration:8 position:ccp(0, uniPos(1000, 2000))];
    CCMoveBy *m2 = [CCMoveBy actionWithDuration:8 position:ccp(0, uniPos(1000, 2000))];
    CCMoveBy *m3 = [CCMoveBy actionWithDuration:8 position:ccp(0, uniPos(1000, 2000))];
    
    
    [spr1 runAction:m1];
    [spr2 runAction:m2];
    [spr3 runAction:m3];
    [sprB runAction:mb];
    [sprV1 runAction:mv1];
    [sprV2 runAction:mv2];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:2];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self step22];
    }];
    
    CCSequence *sequence  = [CCSequence actions:delay, callBlock, nil];
    [self runAction:sequence];
}

-(void) step22
{
    CCNode *spr = [self getChildByTag:100];
    [self text:@"Oh no, i must save them" delay:0.5 object:spr];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:2];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self step23];
    }];
    
    CCSequence *sequence  = [CCSequence actions:delay, callBlock, nil];
    [self runAction:sequence];
}

-(void) step23
{
    CCSprite *spr = (CCSprite *)[self getChildByTag:100];
    CCJumpTo *jumpTo = [CCJumpTo actionWithDuration:4 position:ccp(spr.position.x, winSize.height + uniPos(100, 200)) height:20 jumps:10];
    [spr runAction:jumpTo];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:5];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setBool:YES forKey:KEY_FIRST_TIME_OPEN];
        [defaults synchronize];
        [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.5 scene:[MenuScene scene]]];
    }];
    
    CCSequence *sequence  = [CCSequence actions:delay, callBlock, nil];
    [self runAction:sequence];
}

-(CCLabelBMFont *) text:(NSString *)text delay:(float)d object:(CCNode *)obj
{
    CCLabelBMFont *lb = [CCLabelBMFont labelWithString:text fntFile:FONT_DEFAULT];
    [lb setScale:0];
    [lb setAnchorPoint:ccp(0.5, 0)];
    [lb setPosition:ccp(obj.position.x, obj.position.y + uniPos(15, 30))];
    
    CCDelayTime *delay = [CCDelayTime actionWithDuration:d];
    CCScaleTo *scaleTo = [CCScaleTo actionWithDuration:0.2 scale:1];
    CCDelayTime *dl = [CCDelayTime actionWithDuration:2];
    CCScaleTo *scaleTo2 = [CCScaleTo actionWithDuration:0.2 scale:0];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [lb removeFromParent];
    }];
    CCSequence *sequence = [CCSequence actions:delay, scaleTo, dl, scaleTo2, callBlock, nil];
    
    [self addChild:lb z:2];
    [lb runAction:sequence];
    
    return lb;
}

@end
